#include "physicsrenderer.h"
#include "graphics/colour.h"
#include "math/vector.h"
#include "graphics/debugdraw.h"

namespace gecko
{

PhysicsRenderer g_physicsRenderer;

void PhysicsRenderer::DrawPolygon( const b2Vec2* vertices, int32 vertexCount, const b2Color& color )
{
	gecko::Colour colour = gecko::Colour(color.r, color.g, color.b, 1.0f);

	int i = 0;
	while (i < vertexCount - 1)
	{
		gecko::Vector3f vert1 = gecko::Vector3f(vertices[i].x, vertices[i].y, 0.0f);
		gecko::Vector3f vert2 = gecko::Vector3f(vertices[i+1].x, vertices[i+1].y, 0.0f);
		gecko::DebugDrawManager::Get().Line3D(vert1, vert2, colour, colour);

		i++;
	}

	gecko::Vector3f vert1 = gecko::Vector3f(vertices[i].x, vertices[i].y, 0.0f);
	gecko::Vector3f vert2 = gecko::Vector3f(vertices[0].x, vertices[0].y, 0.0f);
	gecko::DebugDrawManager::Get().Line3D(vert1, vert2, colour, colour);
}

void PhysicsRenderer::DrawSolidPolygon( const b2Vec2* vertices, int32 vertexCount, const b2Color& color )
{
	gecko::Colour colour = gecko::Colour(color.r, color.g, color.b, 1.0f);

	int i = 0;
	while (i < vertexCount - 1)
	{
		gecko::Vector3f vert1 = gecko::Vector3f(vertices[i].x, vertices[i].y, 0.0f);
		gecko::Vector3f vert2 = gecko::Vector3f(vertices[i+1].x, vertices[i+1].y, 0.0f);
		gecko::DebugDrawManager::Get().Line3D(vert1, vert2, colour, colour);

		i++;
	}

	gecko::Vector3f vert1 = gecko::Vector3f(vertices[i].x, vertices[i].y, 0.0f);
	gecko::Vector3f vert2 = gecko::Vector3f(vertices[0].x, vertices[0].y, 0.0f);
	gecko::DebugDrawManager::Get().Line3D(vert1, vert2, colour, colour);
}

void PhysicsRenderer::DrawCircle( const b2Vec2& center, float32 radius, const b2Color& color )
{
	gecko::Colour colour = gecko::Colour(color.r, color.g, color.b, 1.0f);

	gecko::Vector3f vert_centre(center.x, center.y, 0.0f);
	float segmentAngle = (gecko::TWO_PI / NUM_CIRCLE_SEGMENTS);

	for (int i = 0; i < NUM_CIRCLE_SEGMENTS; i++)
	{
		float angle1 = segmentAngle * i;
		float angle2 = angle1 + segmentAngle;

		gecko::Vector3f vert1 = vert_centre + gecko::Vector3f(radius * cosf(angle1), radius * sinf(angle1), 0.0f);
		gecko::Vector3f vert2 = vert_centre + gecko::Vector3f(radius * cosf(angle2), radius * sinf(angle2), 0.0f);

		gecko::DebugDrawManager::Get().Line3D(vert1, vert2, colour, colour);
	}
}

void PhysicsRenderer::DrawSolidCircle( const b2Vec2& center, float32 radius, const b2Vec2& /*axis*/, const b2Color& color )
{
	gecko::Colour colour = gecko::Colour(color.r, color.g, color.b, 1.0f);

	gecko::Vector3f vert_centre(center.x, center.y, 0.0f);
	float segmentAngle = (gecko::TWO_PI / NUM_CIRCLE_SEGMENTS);

	for (int i = 0; i < NUM_CIRCLE_SEGMENTS; i++)
	{
		float angle1 = segmentAngle * i;
		float angle2 = angle1 + segmentAngle;

		gecko::Vector3f vert1 = vert_centre + gecko::Vector3f(radius * cosf(angle1), radius * sinf(angle1), 0.0f);
		gecko::Vector3f vert2 = vert_centre + gecko::Vector3f(radius * cosf(angle2), radius * sinf(angle2), 0.0f);

		gecko::DebugDrawManager::Get().Line3D(vert1, vert2, colour, colour);
	}
}

void PhysicsRenderer::DrawSegment( const b2Vec2& p1, const b2Vec2& p2, const b2Color& /*color*/ )
{
	gecko::DebugDrawManager::Get().Line3D(gecko::Vector3f(p1.x, p1.y, 0.0f), gecko::Vector3f(p2.x, p2.y, 0.0f), gecko::Colour::White(), gecko::Colour::White());
}

void PhysicsRenderer::DrawTransform( const b2Transform& /*xf*/ )
{

}

}